package cn.edu.neosoft.sunbo509.finefood.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neosoft.sunbo509.finefood.Activity.ShopdetailActivity;
import cn.edu.neosoft.sunbo509.finefood.BaseUrl;
import cn.edu.neosoft.sunbo509.finefood.Bean.CollectshopBean;
import cn.edu.neosoft.sunbo509.finefood.Bean.GetcollectBean;
import cn.edu.neosoft.sunbo509.finefood.R;
import cn.edu.neosoft.sunbo509.finefood.Service.Service;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 小博 on 2017/11/5.
 */

public class CollectshopAdapter extends RecyclerView.Adapter {

    private List<GetcollectBean>list;
    private Context context;
    private LayoutInflater layoutInflater;
    private Retrofit retrofit;
    private String userid;
    private int shopid;
    private Service service;

    public CollectshopAdapter(Context context) {
        this.context = context;
        layoutInflater=LayoutInflater.from(context);
    }
    public void setList(List<GetcollectBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }
    public void setUserid(String userid) {
        this.userid = userid;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_collectshop, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        GetcollectBean bean=list.get(position);
       ViewHolder viewHolder=(ViewHolder)holder;
        if (bean==null){
            return;
        }
        Picasso.with(context)
                .load(bean.getPic())
                .placeholder(null)
                .into(viewHolder.iv_pic);
        viewHolder.tv_shopname.setText(bean.getShopname());
        viewHolder.tv_shopaddress.setText(bean.getAddress());
        viewHolder.btn_in.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shopid=list.get(position).getShop_id();
                Intent intent=new Intent(context,ShopdetailActivity.class);
                intent.putExtra("shopid",shopid);
                intent.putExtra("userid",userid);
                context.startActivity(intent);
            }
        });
        viewHolder.btn_out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uncollectshop();
            }
        });
    }

    @Override
    public int getItemCount() {
        if(list==null)
        return 0;
        else
            return list.size();
    }

    //取消收藏店铺
    private void Uncollectshop(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BaseUrl.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service=retrofit.create(Service.class);
        Call<CollectshopBean> call=service.getshopValue(Integer.valueOf(userid) ,shopid);
        call.enqueue(new Callback<CollectshopBean>() {
            @Override
            public void onResponse(Call<CollectshopBean> call, Response<CollectshopBean> response) {
                if(response.body().getSuccess().equals("1"))
                    Toast.makeText(context,"取消成功",Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(context,"取消失败",Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onFailure(Call<CollectshopBean> call, Throwable t) {
                Toast.makeText(context,"异常",Toast.LENGTH_SHORT).show();
            }
        });
    }


    private class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_shopname,tv_shopaddress;
        private Button btn_in ,btn_out;
        private ImageView iv_pic;
        ViewHolder(View itemView) {
            super(itemView);
            iv_pic=(ImageView)itemView.findViewById(R.id.iv_pic);
            tv_shopaddress=(TextView)itemView.findViewById(R.id.tv_shopaddress);
            tv_shopname=(TextView)itemView.findViewById(R.id.tv_shopname);
            btn_in=(Button)itemView.findViewById(R.id.btn_in);
            btn_out=(Button)itemView.findViewById(R.id.btn_out);
        }
    }

}
